package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /*
       添加购物车
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
       /*
       1.为当前购物车设置userid
       然后根据用户id，菜品id或者套餐id进行查询，如果有数据则进行修改，如果没有就进行添加。
       2.菜品id
       3.或者套餐id
       4.添加到数据库中
        */
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> shoplqw = new LambdaQueryWrapper<>();
        shoplqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (shoppingCart.getDishId() != null) {//查看数据库，缺什么咱们后端就想法给他补什么
            shoplqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            shoplqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(shoplqw);
        //以上是查找原先数据库中是否有购物车信息
        if (one == null) {//表示没有,则添加数据
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;//因为one为空
        } else {//否则就是原先就有数据
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    /*
    减少购物车数据-1
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        /*
        取出id，判断是否是菜品还是套餐id，然后设置条件，进行数量-1,当数量等于0时，移除购物车。
         */
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        if (shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);

        if (one != null) {
            Integer number = one.getNumber();
            if (number <= 1) {
                one.setNumber(0);
                log.info("此时的one对象{}.", one);
                LambdaQueryWrapper<ShoppingCart> lqw2 = new LambdaQueryWrapper<>();
                lqw2.eq(ShoppingCart::getId, one.getId());
                shoppingCartService.remove(lqw2);
                return R.success(one);
            }
                one.setNumber(number - 1);
                shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    /*
    根据当前登录用户ID查询购物车列表，并对查询的结果进行创建时间的倒序排序。
     */
    @GetMapping("/list")
    public R getListShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        return R.success(shoppingCartList);
    }
    @DeleteMapping("/clean")
    public R cleanShoppCart(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success(null);
    }

}
